package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.PageResponse;
import com.movie.dto.RoleDTO;
import com.movie.dto.PermissionDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
@Tag(name = "权限管理", description = "角色和权限管理接口")
public class PermissionController {
    
    private final com.movie.service.impl.PermissionServiceImpl permissionService;
    
    @GetMapping("/roles")
    @Operation(summary = "获取角色列表", description = "分页获取角色列表")
    public ApiResponse<PageResponse<RoleDTO>> getRoleList(
            @Parameter(description = "页码，从0开始") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<RoleDTO> rolePage = permissionService.getRoleList(pageable);
        return ApiResponse.success(PageResponse.of(rolePage));
    }
    
    @GetMapping("/roles/{roleId}")
    @Operation(summary = "获取角色详情", description = "根据ID获取角色详情")
    public ApiResponse<RoleDTO> getRoleDetail(
            @Parameter(description = "角色ID") @PathVariable Long roleId) {
        RoleDTO role = permissionService.getRoleDetail(roleId);
        return ApiResponse.success(role);
    }
    
    @PostMapping("/roles")
    @Operation(summary = "创建角色", description = "创建新的角色")
    public ApiResponse<RoleDTO> createRole(
            @Parameter(description = "角色数据") @RequestBody RoleDTO roleDTO) {
        RoleDTO createdRole = permissionService.createRole(roleDTO);
        return ApiResponse.success("角色创建成功", createdRole);
    }
    
    @PutMapping("/roles/{roleId}")
    @Operation(summary = "更新角色", description = "根据ID更新角色信息")
    public ApiResponse<RoleDTO> updateRole(
            @Parameter(description = "角色ID") @PathVariable Long roleId,
            @Parameter(description = "角色数据") @RequestBody RoleDTO roleDTO) {
        RoleDTO updatedRole = permissionService.updateRole(roleId, roleDTO);
        return ApiResponse.success("角色更新成功", updatedRole);
    }
    
    @DeleteMapping("/roles/{roleId}")
    @Operation(summary = "删除角色", description = "根据ID删除角色")
    public ApiResponse<Void> deleteRole(
            @Parameter(description = "角色ID") @PathVariable Long roleId) {
        permissionService.deleteRole(roleId);
        return ApiResponse.success("角色删除成功", null);
    }
    
    @GetMapping("/permissions")
    @Operation(summary = "获取权限列表", description = "获取所有权限列表")
    public ApiResponse<List<PermissionDTO>> getPermissionList() {
        List<PermissionDTO> permissions = permissionService.getPermissionList();
        return ApiResponse.success(permissions);
    }
    
    @PutMapping("/roles/{roleId}/permissions")
    @Operation(summary = "更新角色权限", description = "更新角色拥有的权限")
    public ApiResponse<Void> updateRolePermissions(
            @Parameter(description = "角色ID") @PathVariable Long roleId,
            @Parameter(description = "权限数据") @RequestBody Map<String, Object> permissionData) {
        @SuppressWarnings("unchecked")
        List<Long> permissionIds = (List<Long>) permissionData.get("permissionIds");
        permissionService.updateRolePermissions(roleId, permissionIds);
        return ApiResponse.success("角色权限更新成功", null);
    }
    
    @GetMapping("/users/{userId}/roles")
    @Operation(summary = "获取用户角色", description = "获取用户拥有的角色列表")
    public ApiResponse<List<RoleDTO>> getUserRoles(
            @Parameter(description = "用户ID") @PathVariable Long userId) {
        List<RoleDTO> userRoles = permissionService.getUserRoles(userId);
        return ApiResponse.success(userRoles);
    }
    
    @PostMapping("/users/{userId}/roles")
    @Operation(summary = "分配用户角色", description = "为用户分配角色")
    public ApiResponse<Void> assignUserRoles(
            @Parameter(description = "用户ID") @PathVariable Long userId,
            @Parameter(description = "角色数据") @RequestBody Map<String, Object> roleData) {
        @SuppressWarnings("unchecked")
        List<Long> roleIds = (List<Long>) roleData.get("roleIds");
        permissionService.assignUserRoles(userId, roleIds);
        return ApiResponse.success("用户角色分配成功", null);
    }
}